package com.jin.lazytool.task;

import com.jin.lazytool.entity.IEvent;
import com.jin.lazytool.entity.LoopEndEvent;
import com.jin.lazytool.entity.LoopStartEvent;
import com.jin.lazytool.utils.CurrentStateUtil;
import com.jin.lazytool.utils.JinUtils;
import com.jin.lazytool.utils.JsonUtils;
import com.jin.lazytool.utils.LogUtil;
import javafx.concurrent.Task;

import java.awt.*;
import java.nio.charset.CharacterCodingException;
import java.util.List;
import java.util.*;

public class RunJinTask  {
    private final List<IEvent> list = new ArrayList<>();
    public RunJinTask(int startGroupIndex,int startEventIndex,int endGroupIndex,int endEventIndex){
        JinTask jinTask = CurrentStateUtil.getSelectTask();
        List<JinTaskGroup> groups = jinTask.getGroups();
        Map<String,List<Object>> loopEvents = new HashMap<>();

        for (int i = 0; i < groups.size(); i++) {
            if(i < startGroupIndex){
                continue;
            }else if(i > endGroupIndex){
                break;
            }
            JinTaskGroup item = groups.get(i);
            List<IEvent> events = item.getEvents();
            for (int k = 0; k < events.size(); k++) {
                if(i == startGroupIndex && k < startEventIndex){
                    continue;
                }else if( i == endGroupIndex && k > endEventIndex){
                    break;
                }
                IEvent itemEvent = events.get(k);
                if(itemEvent instanceof LoopStartEvent event){
                    loopEvents.put(event.getLoopNum(), Arrays.asList(event,list.size() - 1));
                }else if(itemEvent instanceof LoopEndEvent endEvent){
                    String loopNum = endEvent.getLoopNum();
                    if(!loopEvents.containsKey(loopNum)){
                        JinUtils.error(String.format("第%s组的第%s个结束循环事件前没有对应的循环开始事件，循环编号%s",i + 1,k + 1,loopNum));
                        return;
                    }else{
                        List<Object> objects = loopEvents.get(loopNum);
                        LoopStartEvent startEvent = (LoopStartEvent)objects.get(0);
                        Integer index = (Integer) objects.get(1);
                        int jumpCount = list.size() - index;
                        startEvent.setJumpCount(jumpCount);
                        startEvent.initHandler();
                        endEvent.setJumpCount(jumpCount);
                        loopEvents.remove(loopNum);
                    }
                }
                list.add(itemEvent);
            }
        }
        if(!loopEvents.isEmpty()){
            JinUtils.error(JsonUtils.serialize(loopEvents.keySet()) + "循环事件没有结束事件");
        }
    }
    public static Robot robot;

    static {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    public void execute(Task<?> task) {
        try{
            for (int i = 0; i < list.size(); i++) {
                if(task.isCancelled()){
                    break;
                }
                i = list.get(i).execute1(robot,i);
            }
        }catch (InterruptedException | CharacterCodingException | NoSuchMethodException e ){
            LogUtil.error("线程执行异常",e);
            throw new RuntimeException("线程执行异常");
        }
    }

}
